package com.dianshang.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dianshang.service.goods.SkuService;
import com.dianshang.utils.WebUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 搜索引擎(ElasticSearch)
 */
@Controller
public class SearchController {

    @Reference
    private SkuService skuService;

    /**
     * 一键导入数据到ES
     * PUT /yuchen
     *         {
     *         "mappings": {
     *         "doc": {
     *         "properties": {
     *         "name": {
     *         "type": "text",
     *         "analyzer": "ik_smart"
     *         },
     *         "price": {
     *         "type": "integer"
     *         },
     *         "image": {
     *         "type": "text"
     *         },
     *         "createTime": {
     *         "type": "date"
     *         },
     *         "spuId": {
     *         "type": "text"
     *         },
     *         "categoryName": {
     *         "type": "keyword"
     *         },
     *         "brandName": {
     *         "type": "keyword"
     *         },
     *         "spec": {
     *         "type": "object"
     *         },
     *         "saleNum": {
     *         "type": "integer"
     *         },
     *         "commentNum": {
     *         "type": "integer"
     *         }
     *       }
     *     }
     *   }
     * }
     */
    @RequestMapping("importES")
    @ResponseBody
    public Integer importES(){
        return skuService.importES();
    }

    /**
     * 搜索引擎(ElasticSearch)：
     * GET /yuchen/doc/_search
     * {
     *   "query": {
     *     "bool": {
     *       "must": [
     *         {
     *           "match": {
     *           "name": "手机" // 关键字
     *           }
     *         },
     *         {
     *           "term": {
     *               "categoryName": "手机" // 分类
     *           }
     *         },
     *         {
     *           "term": {
     *               "brandName": "华为" // 品牌
     *           }
     *         },
     *         {
     *           "term": {
     *               "spec.颜色.keyword": "红色" // 规格
     *           }
     *         }
     *       ],
     *       "filter": {
     *         "range": {
     *           "price": { // 价格
     *             "gt": 50000,
     *             "lte": 100000
     *           }
     *         }
     *       }
     *     }
     *   },
     *   "aggs": {
     *     "yuchen_category": {
     *       "terms": {
     *         "field": "categoryName" // 分组
     *       }
     *     }
     *   },
     *   "from": 0, // 从第一条数据开始查询
     *   "size": 30, // 要查询30条数据
     *   "sort": [
     *     {
     *       "price": { // 根据价格排序
     *         "order": "desc" // 倒叙
     *       }
     *     }
     *   ],
     *   "highlight": { // 名称高亮显示
     *     "fields": {
     *       "name": { // <font color='red'>手机</font>
     *         "pre_tags": "<font color='red'>",
     *         "post_tags": "</font>"
     *       }
     *     }
     *   }
     * }
     */
    @RequestMapping("search")
    public String search(@RequestParam Map<String,String> searchMap, Model model) throws Exception {
        // 处理乱码,在这里面主要处理URL中传递的值为中文
        searchMap = WebUtil.convertCharsetToUTF8(searchMap);
        // search.do?searchKey=手机
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("search.do?");
        for (String key : searchMap.keySet()) { // searchKey=手机
            String value = searchMap.get(key);
            stringBuffer.append(key+"="+value+"&");
        }
        if (!searchMap.containsKey("pageNo")){
            stringBuffer.append("pageNo=1&");
            searchMap.put("pageNo","1");
        }
        if(!searchMap.containsKey("sortOrder")){
            stringBuffer.append("sortOrder=desc&");
            searchMap.put("sortOrder","desc");
        }
        if(!searchMap.containsKey("sort")){
            stringBuffer.append("sort=&");
            searchMap.put("sort","");
        }
        Map resultMap = skuService.search(searchMap);
        model.addAttribute("resultMap",resultMap);
        // 分类过滤查询
        model.addAttribute("url",stringBuffer.toString());
        // 青橙搜索框和取消分类过滤条件
        model.addAttribute("searchMap",searchMap);
        model.addAttribute("pageNo",Integer.parseInt(searchMap.get("pageNo"))); // 当前页
        return "search";
    }

}